<?php

namespace app\base\controller\api;

use app\base\service\MemberService;
use think\admin\Controller;


/**
 * 蝉镜回调
 * Class Login
 * @package app\store\controller
 */
class Chanjing extends Controller
{
    // 创建数字人回调
    public function createAvatarBack()
    {
        $data = input();
        $this->app->log->error("createAvatarBack:".json_encode($data));
        if(isset($data['id'])){
            $save_data = $data;
            $status = 1;
            switch ($save_data['status']){
                case 1:
                    $status = 2;
                    break;
                case 2:
                    $status = 1;
                    break;
                default:
                    $status = 3;
                    break;
            }
            $this->app->db->name('ai_voice')->where(['code'=>$save_data['id'],'type'=>2,'platform_name'=>"蝉镜"])->update([
                'error_msg'=>$save_data['reason']."-".$save_data['err_reason'],
                'status'=>$status,
                'other_data'=>json_encode($save_data)
            ]);
            $this->success('success',$data);
        }else{
            $this->error($data['msg']);
        }

    }


    //  生成口播视频回调
    public function createAvatarVideoBack()
    {
        $data = input();
        $this->app->log->error("createAvatarVideoBack:".json_encode($data));
        if(isset($data['id'])){
            if($data['status']==30){
                //生成成功
                $this->app->db->name("redian_data")->where(['video_id'=>$data['id']])->update([
                    'video_status'=>3,
                    'video_url'=>$data['video_url'],
                    'video_data'=>json_encode($data),
                    'video_duration'=>$data['duration']
                ]);
                $redian_data = $this->app->db->name("redian_data")->where(['video_id'=>$data['id']])->find();
                //加入计费逻辑
                $charge_r = MemberService::instance()->charge($redian_data['user_id'],"videoGenerate",$redian_data,"蝉镜");
                if($charge_r['code']==0){
                    $this->app->db->name("redian_data")->where(['video_id'=>$data['id']])->update([
                        'video_status'=>4,
                        'video_errmsg'=>"生成失败:".$charge_r['msg'],
                        'video_data'=>json_encode($data)
                    ]);
                }
//                if(empty($this->app->db->name('ai_api_config')
//                    ->where(['user_id' => $redian_data['user_id'],'type'=>8])->find())){
//                    //加入计费逻辑
//                    $charge_r = MemberService::instance()->charge($redian_data['user_id'],"videoGenerate",$redian_data,"蝉镜");
//                    if($charge_r['code']==0){
//                        $this->app->db->name("redian_data")->where(['video_id'=>$data['id']])->update([
//                            'video_status'=>4,
//                            'video_errmsg'=>"生成失败:".$charge_r['msg'],
//                            'video_data'=>json_encode($data)
//                        ]);
//                    }
//                }


            }elseif ($data['status']==10){
                $this->app->db->name("redian_data")->where(['video_id'=>$data['id']])->update([
                    'video_status'=>2,
                    'video_data'=>json_encode($data)
                ]);
            }else{
                $this->app->db->name("redian_data")->where(['video_id'=>$data['id']])->update([
                    'video_status'=>4,
                    'video_errmsg'=>$data['msg'],
                    'video_data'=>json_encode($data)
                ]);
            }
            $task = $this->app->db->name("redian_data")->where(['video_id'=>$data['id']])->find();
            if($task){
                $task_id = $task['task_id'];
                $task_run_log_id = $task['task_run_log_id'];
                $num = $this->app->db->name("redian_data")->where(['task_id'=>$task_id,'audio_status'=>3,'task_run_log_id'=>$task_run_log_id])->whereIn('video_status','1,2')->count();
                if($num==0){
                    $s = $this->app->db->name("redian_data")->where(['task_id'=>$task_id,'audio_status'=>3,'video_status'=>3,'task_run_log_id'=>$task_run_log_id])->count();
                    if($s>0){
                        $task_info = $this->app->db->name("redian_task")->where(['id'=>$task_id])->find();
                        $is_auto_fb = $task_info['is_auto_fb'];
                        if($is_auto_fb==1){
                            if(in_array($task_info['type'],[1,4])){
                                //创建推送数据到设备任务
                                sysqueue($task_info['title']."推送数据到设备任务", "xbase:TaskPush",  10, $data = [
                                    'task_id'=>$task_id,'task_run_log_id'=>$task_run_log_id
                                ], $rscript = 1, $loops = 0);
                            }else{
                                $this->app->db->name("redian_push_schedule")->where(['task_id'=>$task_id])->update(['status'=>1]);
                            }
                            $this->app->db->name("redian_task")->where(['id'=>$task_id])->update([
                                'progress'=>6
                            ]);
                            if($task_run_log_id){
                                $this->app->db->name("task_run_log")->where(["id"=>$task_run_log_id])->update(['progress'=>6]);
                            }
                        }else{
                            $this->app->db->name("redian_task")->where(['id'=>$task_id])->update([
                                'progress'=>7,
                                'status'=>1
                            ]);
                            if($task_run_log_id){
                                $this->app->db->name("task_run_log")->where(["id"=>$task_run_log_id])->update(['progress'=>7]);
                            }
                        }

                    }else{
                        $this->app->db->name("redian_task")->where(['id'=>$task_id])->update([
                            'error_msg'=>"视频生成失败",
                            'error_status'=>5,'status'=>1
                        ]);
                        if($task_run_log_id){
                            $this->app->db->name("task_run_log")->where(["id"=>$task_run_log_id])->update(['result'=>"视频生成失败",'error_status'=>5]);
                        }
                    }
                }
            }

        }

    }

    //创建声音回调
    public function createVoiceBack()
    {
        $data = input();
        $this->app->log->error("createAvatarBack:".json_encode($data));
        if(isset($data['id'])){
            $save_data = $data;
            $status = 1;
            if($data['progress']==0){
                $status = 3;
            }elseif ($data['progress']==100){
                $status = 1;
                //加入计费逻辑(暂时弃用20250703)
//                $voice = $this->app->db->name("ai_voice")->where(['code'=>$save_data['id']])->find();
//                if($voice['is_reset']==1){
//                    $charge_r = MemberService::instance()->charge($voice['user_id'],"voiceTemplate",$voice,"蝉镜");
//                    if($charge_r['code']==0){
//                        $this->app->db->name('ai_voice')->where(['code'=>$save_data['id'],'type'=>1,'platform_name'=>"蝉镜"])->update([
//                            'error_msg'=>$charge_r['msg'],
//                            'status'=>3,
//                            'other_data'=>""
//                        ]);
//                        $this->error("蓝豆不足");
//                    }
//                }

            }else{
                $status =2;
            }
            $this->app->db->name('ai_voice')->where(['code'=>$save_data['id'],'type'=>1,'platform_name'=>"蝉镜"])->update([
                'error_msg'=>$save_data['err_msg'],
                'status'=>$status,
                'other_data'=>json_encode($save_data)
            ]);
            $this->success('success',$data);
        }else{
            $this->error($data['msg']);
        }
    }

    //  生成音频回调
    public function createAudioBack()
    {
        $data = input();
        $this->app->log->error("createAudioBack:".json_encode($data));
        if(isset($data['id'])){
            $save_data = $data;
            if($data['status']==9){
                if(isset($data['full']['url'])){
                    $this->app->db->name("redian_data")->where(['audio_id'=>$data['id']])->update([
                        'audio_status'=>3,
                        'audio_url'=>$data['full']['url'],
                        'audio_duration'=>$data['full']['duration'],
                        'audio_data'=>json_encode($data)
                    ]);
                    $redian_data = $this->app->db->name("redian_data")->where(['audio_id'=>$data['id']])->find();
                    $voice_id =$this->app->db->name("redian_task")->where(['id'=>$redian_data['task_id']])->value("voice");
                    $voice = $this->app->db->name("ai_voice")->where(['id'=>$voice_id])->find();
                    $stage_tag = "voiceGenerate";
                    if($voice['model_type']=="cicada3.0"){
                        $stage_tag = "voiceGenerateV2";
                    }

                    //加入计费逻辑
                    $charge_r = MemberService::instance()->charge($redian_data['user_id'],$stage_tag,$redian_data,"蝉镜");
                    if($charge_r['code']==0){
                        $this->app->db->name("redian_data")->where(['audio_id'=>$data['id']])->update([
                            'audio_status'=>4,
                            'audio_errmsg'=>"生成失败:".$charge_r['msg'],
                            'audio_data'=>json_encode($data)
                        ]);
                    }
//                    if(empty($this->app->db->name('ai_api_config')
//                        ->where(['user_id' => $redian_data['user_id'],'type'=>8])->find())){
//                        //加入计费逻辑
//                        $charge_r = MemberService::instance()->charge($redian_data['user_id'],"voiceGenerate",$redian_data,"蝉镜");
//                        if($charge_r['code']==0){
//                            $this->app->db->name("redian_data")->where(['audio_id'=>$data['id']])->update([
//                                'audio_status'=>4,
//                                'audio_errmsg'=>"生成失败:".$charge_r['msg'],
//                                'audio_data'=>json_encode($data)
//                            ]);
//                        }
//                    }
                }else{
                    $this->app->db->name("redian_data")->where(['audio_id'=>$data['id']])->update([
                        'audio_status'=>4,
                        'audio_errmsg'=>"生成失败:".$data['errMsg']."--".$data['errReason']
                    ]);
                }

            }
            $this->success('success',$data);
        }else{
            $this->error($data['errMsg']."--".$data['errReason']);
        }

    }
}
